package com.dc.aimc.user.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dc.aimc.common.service.response.JsonResponse;
import com.dc.aimc.user.dto.AppDTO;
import com.dc.aimc.user.entity.App;
import com.dc.aimc.user.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 第三方应用表 前端控制器
 * </p>
 *
 * @author 冯照龙
 * @since 2021-12-10
 */
@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    private AppService appService;

    /**
     * 添加第三方应用信息
     * @param app
     * @param request
     * @return
     */
    @PostMapping("/app")
    public JsonResponse<App> addApp(@RequestBody App app, HttpServletRequest request) {
        String tenant = request.getHeader("tenant-id");
        app.setTenantId(tenant);
        String appId = UUID.randomUUID().toString();
        app.setAppId(appId);
        String secret = SecureUtil.sha1(appId);
        app.setAppSecret(secret);
        appService.save(app);
        return JsonResponse.success();
    }

    /**
     * 更新第三方应用信息
     * @param app
     * @return
     */
    @PutMapping("/app")
    public JsonResponse<App> updateApp(@RequestBody App app) {
        appService.updateById(app);
        return JsonResponse.success();
    }

    /**
     * 根据ID获取第三方应用信息
     * @param appId
     * @return
     */
    @GetMapping("/app/{id}")
    public JsonResponse<App> getAppByAppId(@PathVariable("id") String appId) {
        LambdaQueryWrapper<App> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(App::getAppId, appId);
        App app = appService.getOne(wrapper);
        return JsonResponse.success(app);
    }

    /**
     * 根据ID删除第三方应用信息
     * @param ids
     * @return
     */
    @DeleteMapping("/app/{ids}")
    public JsonResponse<App> deleteApp(@PathVariable("ids") Integer... ids) {
        appService.removeByIds(Arrays.asList(ids));
        return JsonResponse.success();
    }

    /**
     * 根据dto参数对象获取第三方应用信息
     * @param dto
     * @return
     */
    @PostMapping("/app/dto")
    public JsonResponse<Object> getApp(@RequestBody AppDTO dto) {
        LambdaQueryWrapper<App> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(App::getAppName, dto.getAppName());
        if (dto.getStatus() != null) {
            wrapper.eq(App::getStatus, dto.getStatus());
        }
        if (dto.getIsDeleted() != null) {
            wrapper.eq(App::getIsDeleted, dto.getIsDeleted());
        }
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            IPage<App> page = appService.page(new Page<>(dto.getPageNum(), dto.getPageSize()), wrapper);
            return JsonResponse.success(page);
        }
        List<App> apps = appService.list(wrapper);
        return JsonResponse.success(apps);
    }

}

